package com.example.login.controller;

import com.example.login.entity.ClassInfo;
import com.example.login.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ResponseBody
    @RequestMapping(value = "/chooseCourse", method = RequestMethod.POST)
    public Map<String, Object> chooseCourse(@RequestParam("userName") String userName, @RequestParam("courseName") String course_name, @RequestParam("semester") String semester) {
        if (userName == null || userName.isEmpty() || course_name == null || course_name.isEmpty() || semester == null || semester.isEmpty()) {
            return Collections.singletonMap("status", 400);
        }
        HashMap response = new HashMap();
<<<<<<< HEAD
        // 检查参数是否在UserCourse表中存在
        boolean isExist = courseService.checkIfExistInUserCourse(userName, course_name, semester);
        if (isExist) {
            response.put("status", 400);
            response.put("message", "已经选课，请勿重复操作");
        } else {
            boolean isSuccess = courseService.chooseCourse(userName, course_name, semester);
            if (isSuccess) {
                response.put("status", 200);
                response.put("message", "选课成功");
            }
            else {
                response.put("status", 400);
                response.put("message", "选课失败");
            }
        }

        return response;
    }

=======
        try {
            // 检查参数是否在UserCourse表中存在
            boolean isExist = loginService.checkIfExistInUserCourse(userName, course_name, semester);
            if (isExist) {
                response.put("status", 400);
                response.put("message", "已经选课，请勿重复操作");
            } else {
                boolean isSuccess = loginService.chooseCourse(userName, course_name, semester);
                if (isSuccess) {
                    response.put("status", 200);
                    response.put("message", "选课成功");
                } else {
                    response.put("status", 500);
                    response.put("message", "选课失败");
                }
            }
        } catch (Exception e) {
            response.put("status", 500);
            response.put("message", "服务器内部错误");
        }
        return response;
    }
>>>>>>> 77e1c474be113eb88840feb2f1fc7181bcd81339
    @ResponseBody
    @RequestMapping(value = "/getClassInfoByUserName", method = RequestMethod.GET)
    public Map<String, Object> getClassInfoByUserName(@RequestParam("userName") String userName) {
        HashMap response = new HashMap();
        // 通过userName查询className
        String className = courseService.getClassNameByUserName(userName);
        if (className != null) {
            // 通过className查询ClassInfo表中的对应信息
            ClassInfo classInfo = courseService.getClassInfoByClassName(className);
            if (classInfo != null) {
                response.put("status", 200);
                response.put("message", "查询成功");
                response.put("data", classInfo);
            } else {
                response.put("status", 400);
                response.put("message", "查询失败，未找到对应的班级信息");
            }
        } else {
            response.put("status", 400);
            response.put("message", "查询失败，未找到对应的班级名称");
        }
        return response;
    }

<<<<<<< HEAD
}
=======
    @ResponseBody
    @RequestMapping(value = "/getStudentScore", method = RequestMethod.GET)
    public Map<String, Object> getUserCourseByUserNameOrStudentNumAndSemester(@RequestParam("userName") String userName,  @RequestParam("semester") String semester) {
        List<UserCourse> userCourseList = loginService.getStudentScore(userName, semester);
        HashMap response = new HashMap();
        if (userCourseList != null && !userCourseList.isEmpty()) {
            response.put("status", 200);
            response.put("message", "查询成功");
            response.put("data", userCourseList);
        } else {
            response.put("status", 500);
            response.put("message", "查询失败");
        }
        return response;
    }


}
>>>>>>> 77e1c474be113eb88840feb2f1fc7181bcd81339
